const app = getApp();
Page({
  data: {
    image:'../../resource/images/crame.png',
    images:'../../resource/images/crame.png',
    student: '../../resource/images/crame.png',
    school_name:'',
    major_name:'',
    code:'',
    id:'',
    my_code:0,
    my_phone:0,
    bindphone:'获取验证码',
    list:[],
    identity:true,
    students:false,
    phone: false,
    complete:false,
    successful:false,
    failure:false,
    numList: [{
      name: '身份认证'
    },{
      name: '学生认证'
    },{
      name: '手机认证'
    },{
      name: '完成'
    }],
    name:'',
    number:0,
    num: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options){
    const that = this;
    app.util.request({
      url: 'Index/Index/certificationSchedule',
      data: {
        openid: app.globalData.userInfo.openid,
      },
      header: {
        'content-type': 'application/json'
      },
      success(res){
        that.setData({
          list: res.data
        })
        if (res.data.schedule === 1){
          that.setData({
            identity: false,
            students: true,
          })
          that.numSteps();
        }else if(res.data.schedule === 2){
          that.setData({
            identity: false,
            phone: true,
          })
          for (var i=res.data.schedule;i>0;i--){
            that.numSteps();
          }
        }else if(res.data.schedule === 3){
          if (res.data.is_type === '未审核'){
            that.setData({
              identity: false,
              complete: true,
            })
          }else if(res.data.is_type === '已通过'){
            that.setData({
              identity: false,
              successful: true,
            })
          }else if(res.data.is_type === '未通过'){
            that.setData({
              identity: false,
              failure: true,
            })
          }
          for(var i = res.data.schedule; i > 0; i--){
            that.numSteps();
          }
        }
      }
    })
  },
  code:function(e){
    const that = this;
    that.setData({
      my_code: e.detail.value
    })
  },
  my_phone:function(e){
    const that = this;
    that.setData({
      my_phone:e.detail.value
    })
  },
  bindphone:function(e){
    const that = this;
    const phone = that.data.my_phone;
    const req = /^1[34578]\d{9}$/;
    if (!(req.test(phone))) {
      wx.showToast({
        title: '手机号格式不正确请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    var num = 60;
    var inter = setInterval(function(){
      num--;
      that.setData({
        bindphone: num + "s后重新发送", 
      })
      
      if (num < 0){
        num = 1;
        clearInterval(inter);
        that.setData({
          bindphone: "获取验证码",
        })
      }
    }, 1000)
    var code = "";
    for (var i = 0; i < 4; i++) {
      code += Math.floor(Math.random() * 10);
    }
    app.util.request({
      url: 'Index/Index/getPhone',
      data: {
        openid: app.globalData.userInfo.openid,
        phone: that.data.my_phone,
        code: code
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.Code === 'isv.AMOUNT_NOT_ENOUGH'){
          wx.showToast({
            title: '短信接口'+res.data.Message,
            icon: 'none',
            duration: 2000
          });
          return false;
        }
        console.log(res);
      }
    })
    console.log(code);
    that.setData({
      code: code
    })
  },
  setName:function(e){
    const that = this;
    that.setData({
      name:e.detail.value
    })
  },
  setNumber:function(e){
    const that = this;
    that.setData({
      number: e.detail.value
    })
  },
  school:function(e){
    const that = this;
    that.setData({
      school_name: e.detail.value
    })
  },
  major:function(e){
    const that = this;
    that.setData({
      major_name: e.detail.value
    })
  },
  formSubmitIdentity:function(e){
    const that = this;
    if (that.data.name === ''){
      wx.showToast({
        title: '真实姓名不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    const id_card = that.data.number;
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (!reg.test(id_card)){
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.image === '../../resource/images/crame.png'){
      wx.showToast({
        title: '身份证正面照不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.images === '../../resource/images/crame.png') {
      wx.showToast({
        title: '身份证反面照不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    
    app.util.request({
      url: 'Index/Index/setIdentity',
      data:{
        openid: app.globalData.userInfo.openid,
        name: that.data.name,
        sex: e.detail.value.sex,
        id_card: that.data.number,
        positive_card: that.data.image,
        reverse_card: that.data.images
      },
      method:'post',
      header: {
        'content-type': 'application/json'
      },
      success(res){
        var data = res.data;
        if (res.data.code === 0){
          that.setData({
            identity: false,
            students: true,
            id:data.data
          })
          that.numSteps();
        }
      }
    })
  },
  formSubmitStudents:function(e){
    const that = this;
    var id = '';
    if(that.data.id !== ''){
      id = that.data.id;
    }else{
      id = that.data.list.id;
    }
    if (that.data.school_name === '') {
      wx.showToast({
        title: '学校名称不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.major_name === '') {
      wx.showToast({
        title: '专业名称不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.student === '../../resource/images/crame.png') {
      wx.showToast({
        title: '学生证照片不能为空',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    app.util.request({
      url: 'Index/Index/setStudents',
      data: {
        openid: app.globalData.userInfo.openid,
        id: id,
        school_name: that.data.school_name,
        major_name: that.data.major_name,
        student: that.data.student
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success(res){
        if (res.data.code === 0){
          that.setData({
            students: false,
            phone: true,
          })
          that.numSteps();
        }
      }
    })
  },
  formSubmitPhone:function(e){
    const that = this;
    var id = '';
    if (that.data.id !== '') {
      id = that.data.id;
    } else {
      id = that.data.list.id;
    }
    const phone = that.data.my_phone;
    const req = /^1[34578]\d{9}$/;
    if (!(req.test(phone))){
      wx.showToast({
        title: '手机号格式不正确请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    if (that.data.my_code !== that.data.code){
      wx.showToast({
        title: '验证码不正确请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    app.util.request({
      url: 'Index/Index/setPhone',
      data: {
        openid: app.globalData.userInfo.openid,
        id: id,
        phone: that.data.my_phone
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        that.setData({
          phone: false,
          complete: true,
        })
        that.numSteps();
      }
    })
  },
  numSteps(){
    this.setData({
      num: this.data.num == this.data.numList.length - 1 ? 0 : this.data.num + 1
    })
  },
  bindChooiceProduct:function(res){
    var that = this;
    const style = res.currentTarget.dataset.style;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数  
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album','camera'], 
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        let uploadUrl = app.util.url('index/index/doPageWxUpload');
        for (var i=0,h=tempFilePaths.length;i<h;i++){
          wx.uploadFile({
            url: uploadUrl,
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {
              'imgIndex': i
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              uploadImgCount++;
              if (style === 'image'){
                that.setData({
                  image: res.data
                })
              } else if (style === 'images'){
                that.setData({
                  images: res.data
                })
              }else{
                that.setData({
                  student: res.data
                })
              }
              wx.showToast({
                title: '图片上传成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail:function(res){
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function(res){

                }
              })
            }
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})